package com.woniu.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.woniu.adapter.ProcessHandoverAdapter;
import com.woniu.adapter.ProgressAdapter;
import com.woniu.adapter.WorkReportAdapter;
import com.woniu.outlet.client.OrderClient;
import com.woniu.outlet.client.PlanClient;
import com.woniu.outlet.po.ProPlanPo;
import com.woniu.outlet.po.ProcessHandover;
import com.woniu.outlet.po.Progress;
import com.woniu.service.WorkReportService;
import com.woniu.util.JwtTokenUitl;
import com.woniu.util.ResponseResult;
import com.woniu.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WorkReportServiceImpl implements WorkReportService {
    @Autowired
    private ProcessHandoverAdapter processHandoverAdapter;
    @Autowired
    private WorkReportAdapter workReportAdapter;
    @Autowired
    private ProgressAdapter progressAdapter;
    @Autowired
    private PlanClient planClient;
    @Autowired
    private OrderClient orderClient;

    @Override
    public List<ProcessHandover> getWorkReport (String OrderCode) throws JsonProcessingException {
        return  processHandoverAdapter.getWorkReport(OrderCode);
    }

    /** 报工
     * @param processHandover
     */
    @Override
    public void workReport (ProcessHandover processHandover) {
        ProcessHandover processHandover2 =  processHandoverAdapter.getProcessHandoverById(processHandover.getId());
        processHandover2.setBeHandoverName(processHandover2.getBeHandoverName());
        processHandover2.setSpare1("已报工");
        processHandover2.setSpare2(TimeUtil.getNowTime(new Date()));
        processHandover2.setSpare3(processHandover.getSpare3());
        Progress progress = new Progress();
//        需要从生产计划那边查询,根据订单编号查询生产计划id
        Long body = orderClient.findIdByOrderNo(processHandover2.getOrderCode()).getData();
        ResponseResult<ProPlanPo> allProPlan = planClient.findAllProPlan(body);
        Long planNo = allProPlan.getData().getPlanNo();
        processHandover2.setProPlanId(planNo);

        progressAdapter.passiveUser(planNo,processHandover2.getId(),processHandover2.getBehandoverPersonId());
        workReportAdapter.workReport(processHandover2);
    }
}
